<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>保单编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
    <style type="text/css">
		.layui-form{
			padding: 20px ;
		}
        .layui-form-item {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }
    </style>
</head>
<body class="childrenBody">
		<form class="layui-form layui-form-pane">
	
		<div class="layui-form-item">
			    <label class="layui-form-label">保险公司</label>
			    <div class="layui-input-block">
			      <select name="companyType" lay-verify="required" id="companyType">
					  <option value="">--请选择保险公司--</option>
					  <option value="1">中国人保</option>
					  <option value="2">中国人寿</option>
					  <option value="3">中国平安</option>
					  <option value="4">太平洋保险</option>
					  <option value="5">中国太平</option>
					  <option value="6">友邦保险</option>
					  <option value="7">新华保险</option>
					  <option value="8">泰康保险</option>
					  <option value="9">阳光保险</option>
					  <option value="10">大地车险</option>
					  <option value="11">天安车险</option>
					  <option value="12">安邦车险</option>
					  <option value="13">华泰车险</option>
					  <option value="14">安盛天平车险</option>
					  <option value="15">中银车险</option>
					  <option value="16">永诚车险</option>
			      </select>
			    </div>
	  	</div>
		<div class="layui-form-item">
			<label class="layui-form-label">保单号</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" lay-verify="required" name="code" placeholder="请输入保单号">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">客户</label>
			<div class="layui-input-inline">
				<select name="customerId" lay-verify="required" lay-filter="test" lay-search id="customerId">
				 	<option value="">--请选择客户--</option>
				</select> 
			<!--<span>请根据手机号码或姓名检索</span>-->
			</div>
			<div class="layui-form-mid layui-word-aux">请根据手机号码或姓名检索</div>
		</div>

		
		<div class="layui-form-item">
			<label class="layui-form-label">投保车辆</label>
			<div class="layui-input-block">
				<select name="customerCarId" lay-verify="required" id="carInfoId">
				 	<option value="">--请选择车辆信息--</option>
				</select> 
			</div>
		
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">投保日期</label>
			<div class="layui-input-inline">
				<input type="text" id="test1"  class="layui-input" lay-verify="required" name="expiryTime" placeholder="请选择保单生效日期">
			</div>

			<div class="layui-form-mid layui-word-aux"><span> 0时起  至 </span><span id="end_time"></span></div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">投保金额</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input"  name="policyMoney" placeholder="请输入投保金额">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">礼包金额</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input"  name="returnMoney" placeholder="请输入礼包金额">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">投保人</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="policyName" placeholder="投保人">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">投保人手机号码</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="policyPhone" placeholder="投保人手机号码">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="editPolicys">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</div>
	</form>

<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script type="text/javascript">

layui.config({
    base: '/js/admin/'
}).extend({
    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
    $tool: 'tool'
}).use(['form', 'layer', 'tree', 'jquery', 'ajaxExtention','laydate', '$tool'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        $tool = layui.$tool;
    
	  var membersList;
	  $.ajax({
          type : "GET",
          url: '/storeMember/getAllStoreMembers',
          dataType:'json',
       	  async:false,
          success: function(data){
          	var list=data.data;
          	membersList=list;
          	var html='';
              for(var i=0;i<list.length;i++){	
                  html += "<option value='"+list[i].id+"'>"+list[i].phone+"   "+list[i].name+"</option>"
              }
              $("#customerId").append(html);
              form.render();
          }
      }); 
	  form.on('select(test)', function(data){
		  if(data.value!=''){
			  $.ajax({
                  type : "GET",
                  url: '/storeMemberCar/getCarsByStoreMember/'+data.value,
		          dataType:'json',
		       	  async:false,
		          success: function(data){
		          	var list=data.data;
		          	membersList=list;
		        	var html='<option value="">--请选择车辆信息--</option>';
		              for(var i=0;i<list.length;i++){	
		                  html += "<option value='"+list[i].id+"'>"+list[i].plateNumber+"   "+list[i].carInfo+"</option>"
		              }
		              $("#carInfoId").html(html);
		              form.render();
		          }
		      });
		  }
		  return false;
        	
      }); 
	  
	  
	  function triggerMemberCar(id){
		  $.ajax({
              type : "GET",
              url: '/storeMemberCar/getCarsByStoreMember/'+id,
	          dataType:'json',
	       	  async:false,
	          success: function(data){
	          	var list=data.data;
	          	membersList=list;
	          	var html='';
	              for(var i=0;i<list.length;i++){	
	                  html += "<option value='"+list[i].id+"'>"+list[i].plateNumber+"   "+list[i].carInfo+"</option>"
	              }
	              $("#carInfoId").append(html);
	              form.render();
	          }
	      });
	  }
	  initUserInfo();
     
    /**
     * 初始化用户信息
     * */
     function initUserInfo() {
        var queryArgs = $tool.getQueryParam();//获取查询参数
        var id = queryArgs['id'];
        var url = '/policys/getById/'+id;
        $.ajax({
            url:url,
            method:"GET",
            success:function (data) {
                var datas = data.data;
               $("input[name='code']").val(datas.code);
               $("#companyType").val(datas.companyType);
               $("#customerId").val(datas.customerId);
               triggerMemberCar(datas.customerId);
               $("select[name='customerCarId'] option[value='"+datas.customerCarId+"']").attr("selected","selected");
                $("input[name='returnMoney']").val(datas.returnMoney);
                $("input[name='policyMoney']").val(datas.policyMoney);
                var expireTime=$tool.formatDate(datas.expiryTime);
            	var selectedDate=new Date(datas.expiryTime); 
		    	 var m = selectedDate.getMonth();  
		    	 selectedDate.setFullYear(selectedDate.getFullYear() + 1);  
				    if (m < selectedDate.getMonth()) {  
				    	selectedDate.setDate(0);  
				    }  
		    	 var text=$tool.formatDate(selectedDate);
		    	 $("#end_time").html(text+" 24时止");
                
                laydate.render({
            	    elem: '#test1'
            	    ,value: expireTime,
            	    done: 
    			    	function(value, date){
    			    	var selectedDate=new Date(date.year,date.month-1,date.date); 
    			    	 var m = selectedDate.getMonth();  
    			    	 selectedDate.setFullYear(selectedDate.getFullYear() + 1);  
    					    if (m < selectedDate.getMonth()) {  
    					    	selectedDate.setDate(0);  
    					    }  
    			    	 var text=$tool.formatDate(selectedDate);
    			    	 $("#end_time").html(text+" 24时止")
    			    	 form.render();  
    			    	}
            	});
                form.render();//重新绘制表单，让修改生效 
            }
        });
       
    }
  
    /**
     * 表单提交
     * */
     form.on("submit(editPolicys)", function (data) {
        var queryArgs = $tool.getQueryParam();//获取查询参数
        var id = queryArgs['id'];
        var companyType = data.field.companyType;
        var code = data.field.code;
        var customerId = data.field.customerId;
        var customerCarId = data.field.customerCarId;
        var expiryTime = data.field.expiryTime;
        var returnMoney = data.field.returnMoney;
        var policyMoney= data.field.policyMoney;
        //请求
        var url ='/policys';
        var req = {
            id:id,
            companyType: companyType,
            code: code,
            customerId: customerId,
            customerCarId: customerCarId,
            expiryTime: expiryTime,
            returnMoney: returnMoney,
            policyMoney:policyMoney
        };
        $.ajax({
            url: url,
            contentType:"application/json;charset=UTF-8",
            data: JSON.stringify(req),
            method: "PUT",
            success: function (data) {
            	if(data.code==2000){
            		 layer.msg("保单信息更新成功！", {time: 1000}, function () {
            			 layer.closeAll("iframe");
                         //刷新父页面
                         parent.location.reload();
                     });
            	}else{
            		layer.msg(data.msg);
            	}
            }
           
        });

        return false;
    }) 

});

</script>
</body>
</html>

